package com.ruoyi.web.controller.model;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.thread.ThreadUtil;
import com.ruoyi.model.manager.input.ModelManagerInput;
import com.ruoyi.model.manager.output.ModelManagerOutput;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.model.manager.domain.ModelManager;
import com.ruoyi.model.manager.service.IModelManagerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 模型管理Controller
 * 
 * @author ruoyi
 * @date 2024-11-08
 */
@RestController
@RequestMapping("/model/manager")
public class ModelManagerController extends BaseController
{
    @Autowired
    private IModelManagerService modelManagerService;

    /**
     * 查询模型管理列表
     */
    @PreAuthorize("@ss.hasPermi('model:manager:list')")
    @GetMapping("/list")
    public TableDataInfo list(ModelManager modelManager)
    {
        startPage();
        List<ModelManager> list = modelManagerService.selectModelManagerList(modelManager);
        return getDataTable(list);
    }

    /**
     * 导出模型管理列表
     */
    @PreAuthorize("@ss.hasPermi('model:manager:export')")
    @Log(title = "模型管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ModelManager modelManager)
    {
        List<ModelManager> list = modelManagerService.selectModelManagerList(modelManager);
        ExcelUtil<ModelManager> util = new ExcelUtil<ModelManager>(ModelManager.class);
        util.exportExcel(response, list, "模型管理数据");
    }

    /**
     * 获取模型管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('model:manager:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(modelManagerService.selectModelManagerById(id));
    }

    /**
     * 新增模型管理
     */
    @PreAuthorize("@ss.hasPermi('model:manager:add')")
    @Log(title = "模型管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ModelManager modelManager)
    {
        return toAjax(modelManagerService.insertModelManager(modelManager));
    }

    /**
     * 修改模型管理
     */
    @PreAuthorize("@ss.hasPermi('model:manager:edit')")
    @Log(title = "模型管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ModelManager modelManager)
    {
        return toAjax(modelManagerService.updateModelManager(modelManager));
    }

    /**
     * 删除模型管理
     */
    @PreAuthorize("@ss.hasPermi('model:manager:remove')")
    @Log(title = "模型管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(modelManagerService.deleteModelManagerByIds(ids));
    }

    /**
     * 导入 Python 文件
     *
     * @param file 上传的 Python 文件
     */
    @PostMapping("/importPythonFile")
    public AjaxResult importPythonFile(@RequestParam("file") MultipartFile file) {
        ModelManagerOutput output = modelManagerService.importPythonFile(file);
        return success(output);
    }

    @PostMapping("/preview")
    public AjaxResult previewModel(@RequestBody ModelManagerInput input) {

        return  success(modelManagerService.previewModel(input));
    }

    @PostMapping("/save")
    public AjaxResult save(@RequestBody ModelManagerInput input) {

        return toAjax(modelManagerService.save(input));
    }

    @PostMapping("/update")
    public AjaxResult update(@RequestBody ModelManagerInput input) {

        return toAjax(modelManagerService.update(input));
    }

    /**
     * 获取模型管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('model:manager:query')")
    @GetMapping(value = "/getInfoById/{id}")
    public AjaxResult getInfoById(@PathVariable("id") Long id)
    {
        return success(modelManagerService.selectModelManagerOutputById(id));
    }

    /**
     * 运行模型
     *
     * @param id
     * @return
     */
    @GetMapping("/runModel/{id}")
    public AjaxResult runModel(@PathVariable("id") long id) {

        ThreadUtil.execAsync(() -> {
            try {
                modelManagerService.runModel(id);
            } catch (Exception e) {
                logger.warn("" + id, e);
            }
        });
        return success("开始分析,请稍后查看分析结果");
    }

}
